package com.asm.DBloader;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ASM_FExpTempDS {
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;

	
	public ASM_FExpTempDS (Context context)
	{
		this.context = context;
		dbHelper = new ASM_DB(context);
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	
	public int Create(ASM_FExpTemp det) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		ContentValues values = new ContentValues();
		values.put(ASM_DB.asmExpTEmp_RefNo, det.getAsmExpTEmp_RefNo());
		values.put(ASM_DB.asmExpTEmp_ExpCode,det.getAsmExpTEmp_ExpCode());
		values.put(ASM_DB.asmExpTEmp_ExpName, det.getAsmExpTEmp_ExpName());
		values.put(ASM_DB.asmExpTEmp_amunt, det.getAsmExpTEmp_amunt());

		
		count = (int) dB.insert(ASM_DB.ASM_EXPTEMP, null, values);
		System.out.println(count);
		dB.close();
		return count;
		
	}
	
	
	public ArrayList<String> getExpTemp() 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select refno,expname,expcode,amunt from Fexptemp";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
			Details.add(cursor.getString(0)+"-:-"+cursor.getString(1)+"-:-"+cursor.getString(2)+"-:-"+cursor.getString(3));
		}
		
		return Details;
	}
	
	
	public ArrayList<String> getExpTemp1(String expcode) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select refno,expname,expcode,amunt from Fexptemp where expcode = '"+expcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
			Details.add(cursor.getString(0)+"-:-"+cursor.getString(1)+"-:-"+cursor.getString(2)+"-:-"+cursor.getString(3));
		}
		
		return Details;
	}
	
	public int Updatetemp(ASM_FExpTemp det) 
	{
		
		int count =0;
		
		if(dB == null)
		{
			open();
		}else if(!dB.isOpen())
		{
			open();
		}

		ContentValues values = new ContentValues();
		
		values.put(ASM_DB.asmExpTEmp_RefNo, det.getAsmExpTEmp_RefNo());
		values.put(ASM_DB.asmExpTEmp_ExpCode,det.getAsmExpTEmp_ExpCode());
		values.put(ASM_DB.asmExpTEmp_ExpName, det.getAsmExpTEmp_ExpName());
		values.put(ASM_DB.asmExpTEmp_amunt, det.getAsmExpTEmp_amunt());
		
		
	
		count = (int) dB.update(ASM_DB.ASM_EXPTEMP, values, "expcode = '"+det.getAsmExpTEmp_ExpCode()+"' ", null);

		dB.close();
		return count;
		
	}
	public int Delete(String expcode)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_EXPTEMP, "expcode = '"+expcode+"'", null);

		dB.close();
		return count;
		
	}
	
	public int Delete1(String REfNo)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_EXPTEMP, "RefNo = '"+REfNo+"'", null);

		dB.close();
		return count;
		
	}
	public String getEmptyOrnot(String RefNo) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		String Details = null;
		
	
		String selectQuery = "select * from Fexptemp where RefNo = '"+RefNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
			Details=cursor.getString(0);
		}
		
		return Details;
	}
	
	public String getsaveddet(String RefNo,String expcode) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		String Details = null;
		
	
		String selectQuery = "select * from Fexptemp where RefNo = '"+RefNo+"' and expcode = '"+expcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
			Details=cursor.getString(1);
		}
		
		return Details;
	}
	
	

}
